# -*- coding: UTF-8 -*-
import traceback
import functools
import tkinter as tk
from Code.View.sc_module import WidgetTip, ScrollFrame, center_window
from Code.View.Datamation.sc_provide import view_gate


class PageDataError(Exception):
    pass


class NotInputError(Exception):
    pass


class UpdateResultError(Exception):
    pass


def wrapper_exception(func):
    """ 异常捕获, 此方法只包装异常, 完善异常信息 """
    @functools.wraps(func)
    def wrapper(self, *args, **kwargs):
        try:
            return func(self, *args, **kwargs)
        except PageDataError as e:
            err = '{}:\n{}'.format(view_gate.query_language_data('DATA', 'EXCEPTION'), e)
        except NotInputError as e:
            err = '{}:\n{}'.format(view_gate.query_language_data('INPUT', 'EXCEPTION'), e)
        except UpdateResultError as e:
            err = '{}:\n{}'.format(view_gate.query_language_data('UPDATE', 'EXCEPTION'), e)
        except Exception as e:
            print(traceback.format_exc())
            err = 'Unknown Exception:\n{}'.format(str(e))

        # 到这里肯定是异常了, 因为try中return了
        WidgetTip.error(err)
        view_gate.exception_data.set_data(traceback.format_exc())

    return wrapper


class AppBase(object):
    """ 子窗体的基类 """
    master = None

    def init_windows(self):
        pass

    def destroy(self):
        self.master.destroy()
        del self

    def pack(self):
        self.init_windows()


class TopWinBase(object):
    """ 弹窗界面的父类, 单例 """
    master = None
    _instance = None

    def __new__(cls, *args, **kw):
        if cls._instance is None:
            cls._instance = object.__new__(cls, *args, **kw)
        return cls._instance

    def show(self, *args, **kw):
        if self.master:
            self.master.wm_attributes('-topmost', 1)
            return
        self.master = tk.Toplevel()
        self.master.title('')
        self.master.protocol("WM_DELETE_WINDOW", self.close)
        self.pack(*args, **kw)

    def pack(self, *args, **kw):
        pass

    def resize(self, w, h):
        self.master.geometry('{}x{}'.format(w, h))
        self.master.resizable(False, False)
        center_window(self.master)

    def topping(self, top='True'):
        if not self.master: return
        n = 1 if top else 0
        self.master.wm_attributes('-topmost', n)

    def close(self):
        if not self.master: return
        self.master.destroy()
        self.master = None


class PageBase(object):
    interface = None
    master = None
    title = None
    width = None
    height = None
    frame = None
    _showing = False
    _page_fm = None

    def _init(self):
        self._showing = True
        self.frame = ScrollFrame(self.master, self.width, self.height).master()

    def pack(self):
        self._init()
        self.pack_frame()

    def pack_frame(self):
        pass

    def alive(self):
        return self._showing

    def destroy_frame(self):
        pass

    def destroy(self):
        self._showing = False
        try:
            self.destroy_frame()
            for ch in self.master.winfo_children():
                ch.destroy()
        except Exception:
            pass

